﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using Lbb.Localization;

namespace Lbb.URating.Controllers
{
    /// <summary>
    /// 点击器控制器
    /// </summary>
    /// <author>Chiron</author>
    public class U_ClickController : URatingControllerBase
    {
        #region Modules
        /// <summary>
        /// 评分分类选择列表
        /// </summary>
        /// <param name="key">键</param>
        /// <param name="categoryId">评分分类</param>
        /// <param name="value">选中值</param>
        /// <param name="hasEmpty">是否有空值项</param>
        /// <param name="emptyTitle">空值项标题</param>
        /// <returns></returns>
        public ActionResult SelectList(string key, Guid? categoryId = null, string value = null, bool hasEmpty = false, string emptyTitle = null)
        {
            if (string.IsNullOrEmpty(key)) key = "clickId";
            if (string.IsNullOrEmpty(emptyTitle)) emptyTitle = OperatorRes.PleaseSelect;
            var data = U_ClickRepository.GetAll();
            if (categoryId.HasValue) data = data.Where(d => d.CategoryID == categoryId.Value);
            var list = data.Select(d => new { d.ClickID, d.Title }).ToList()
                .Select(d => new SelectListItem { Text = d.Title, Value = d.ClickID.ToString() }).ToList(); ;

            if (hasEmpty) list.Insert(0, new SelectListItem { Text = emptyTitle, Value = "" });

            var sel = list.FirstOrDefault(d => d.Value == value);
            if (sel != null) sel.Selected = true;

            ViewData[key] = list;

            return View("SelectList", key as object);
        }
        #endregion Modules
    }
}
